from django.test import TestCase
from rest_framework.test import APIClient
from django.urls import reverse
from user.views import *
from play.models import Play
from datetime import date, timedelta
from user.models import UserProfile
from favor.models import Favor
from follow.models import Follow
from comment.models import Comment
from like.models import Like


# Create your tests here.
class TestUser(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user1 = UserProfile.objects.create_user(id=1,username='test1',password='test1234')
        self.user2 = UserProfile.objects.create_user(id=2,username='test2',password='test5678')
        self.user3 = UserProfile.objects.create_user(id=3,username='I have no videos',password='blablabla')
        self.user4 = UserProfile.objects.create_user(id=4,username='I will change',password='change')
        self.video1 = Videos.objects.create(vid = 1, duration=10, mup_id = 1)
        self.video2 = Videos.objects.create(vid = 2, duration = 100, mup_id = 1)
        self.play1 = Play.objects.create(pid = 1, video = self.video1, count = 10, user = self.user1)
        self.like1 = Like.objects.create(lid = 1, user_id = self.user2, video_id = self.video1)
        self.like2 = Like.objects.create(lid = 3, user_id = self.user3, video_id = self.video1)
        self.favor1 = Favor.objects.create(fid=1, user_id=self.user2, video_id=self.video1) #二号用户给一号用户的一号视频点赞
        self.favor2 = Favor.objects.create(fid=2, user_id=self.user3, video_id=self.video1) #三号用户给一号用户的一号视频点赞
        self.follow1 = Follow.objects.create(foid=1, fan=self.user2, mup=self.user1)
        self.follow2 = Follow.objects.create(foid=2, fan=self.user3, mup=self.user1)
        self.comment1 = Comment.objects.create(cid=1, user=self.user2, video=self.video1)
        self.comment2 = Comment.objects.create(cid=2, user=self.user3, video=self.video1)

    def test_get_user_info_success(self):
        url = reverse('user_info', kwargs={'user_id': self.user1.id})
        response = self.client.get(url)
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data['id'], 1)

    def test_get_user_info_fail(self):
        url = reverse('user_info', kwargs={'user_id': self.user1.id+1})
        try:
            self.client.get(url)
        except UserProfile.DoesNotExist:
            return

    def test_get_create_data_success(self):
        url = reverse('get_create_data', kwargs={'user_id': self.user1.id, 'date_op': '7日内'})
        response = self.client.get(url)
        self.assertEqual(response.status_code,200)

    def test_get_create_data_fail(self):
        url = reverse('get_create_data', kwargs={'user_id': self.user1.id, 'date_op': '8日内'})
        try:
            response = self.client.get(url)
        except TypeError:
            return

    def test_get_search_result_success(self):
        url=reverse('searchUser')
        response = self.client.get(url, {'name':'test','page':1})
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data['total_count'],2)

    def test_get_search_result_fail(self):
        url = reverse('searchUser')
        response = self.client.get(url, {'name':'testmmmmmm','page':1})
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data['total_count'], 0)

    def test_get_chart_data_success(self):
        url = reverse('get_chart_data', kwargs={'user_id': self.user1.id, 'date_op':'7日内', 'choice':1})
        response = self.client.get(url)
        self.assertEqual(response.status_code,200)

    def test_get_chart_data_fail(self):
        url = reverse('get_chart_data', kwargs={'user_id':self.user1.id, 'date_op':'7日内','choice':5})
        try:
            response = self.client.get(url)
        except IndexError:
            return

    def test_get_mup_data_success(self):
        url = reverse('get_mup_data', kwargs={'mup_id':1})
        response = self.client.get(url)
        self.assertEqual(response.status_code,200)
        self.assertNotEqual(response.data['data'][0], None)

    def test_get_mup_data_fail(self):
        url = reverse('get_mup_data', kwargs={'mup_id':3})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['data'], [])

    def test_modify_user_success(self):
        url = reverse('modify_user')
        response = self.client.put(url, {'user_id':4,'username':'test_modify_username_1','password':'test_modify_password_1','description':'我是天才'})
        self.assertEqual(response.status_code,200)
        self.assertEqual(response.data['msg'],'修改成功')

    def test_modify_user_fail(self):
        url = reverse('modify_user')
        response = self.client.put(url, {'user_id':4,'username':'test1'})
        self.assertEqual(response.status_code, 400)

    def test_create_success(self):
        url = reverse('signup')
        response = self.client.post(url, {'username':'create','password':'create_psw'})
        self.assertEqual(response.data['username'],'create')

    def test_create_fail(self):
        url = reverse('signup')
        response = self.client.post(url, {'username':'test1','password':'create_psw'})
        self.assertEqual(response.status_code, 400)

    def test_user_exists_success(self):
        url = reverse('login', kwargs={'username':'test1','password':'test1234'})
        response = self.client.get(url)
        self.assertEqual(response.data['username'],'test1')

    def test_user_exists_fail(self):
        url = reverse('login', kwargs={'username':'test_not_exist','password':'skgrbnhqwak'})
        response = self.client.get(url)
        self.assertEqual(response.data['error'],'用户名或密码错误')

    def test_get_data_success(self):
        result = get_data(self.user1, '7日内', choice=0)
        self.assertEqual(result[date.today().strftime('%Y-%m-%d')], 10)

    def test_get_data_fail(self):
        result = get_data(self.user1, '7日内', choice=0)
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')], 10000)

    def test_calculate_time_success(self):
        result = calculate_time()
        today = date.today()
        self.assertEqual(result[0],today)
        self.assertEqual(result[1],today-timedelta(days=7))
        self.assertEqual(result[2],today-timedelta(days=30))
        self.assertEqual(result[3],today.replace(year=today.year-1))

    def test_calculate_time_fail(self):
        result = calculate_time()
        today = date.today()
        self.assertNotEqual(result[0],today.replace(day=today.day-1))
        self.assertNotEqual(result[0],today.replace(day=today.day+1))
        self.assertNotEqual(result[1], today - timedelta(days=8))
        self.assertNotEqual(result[1], today - timedelta(days=6))
        self.assertNotEqual(result[1], today - timedelta(days=29))
        self.assertNotEqual(result[1], today - timedelta(days=31))
        self.assertNotEqual(result[3], today.replace(year=today.year - 2))
        self.assertNotEqual(result[3], today)

    def test_get_week_play_success(self):
        result = get_week_play(Videos.objects.filter(mup_id = 1))
        self.assertEqual(result[date.today().strftime('%Y-%m-%d')], 10)

    def test_get_week_play_fail(self):
        result = get_week_play(Videos.objects.filter(mup_id = 1))
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')], 11)
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')], 9)

    def test_get_week_favor_success(self):
        result = get_week_favor(Videos.objects.filter(mup_id = 1))
        self.assertEqual(result[date.today().strftime('%Y-%m-%d')],2)

    def test_get_week_favor_fail(self):
        result = get_week_favor(Videos.objects.filter(mup_id=1))
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')],1)
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')],3)

    def test_get_week_fan_success(self):
        result = get_week_fan(self.user1)
        self.assertEqual(result[date.today().strftime('%Y-%m-%d')],2)

    def test_get_week_fan_fail(self):
        result = get_week_fan(self.user1)
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')], 1)
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')], 3)

    def test_get_week_comment_success(self):
        result = get_week_comment(Videos.objects.filter(mup_id=1))
        self.assertEqual(result[date.today().strftime('%Y-%m-%d')], 2)

    def test_get_week_comment_fail(self):
        result = get_week_comment(Videos.objects.filter(mup_id=1))
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')], 1)
        self.assertNotEqual(result[date.today().strftime('%Y-%m-%d')], 3)

    def test_get_month_play_success(self):
        result = get_month_play(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertEqual(result['{} - {}'.format(today,today)], 10)

    def test_get_month_play_fail(self):
        result = get_month_play(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertNotEqual(result['{} - {}'.format(today,today)], 11)
        self.assertNotEqual(result['{} - {}'.format(today,today)], 9)

    def test_get_month_like_success(self):
        result = get_month_like(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertEqual(result['{} - {}'.format(today, today)], 2)

    def test_get_month_like_fail(self):
        result = get_month_like(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertNotEqual(result['{} - {}'.format(today, today)], 1)
        self.assertNotEqual(result['{} - {}'.format(today, today)], 3)

    def test_get_month_favor_success(self):
        result = get_month_favor(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertEqual(result['{} - {}'.format(today,today)], 2)

    def test_get_month_favor_fail(self):
        result = get_month_favor(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertNotEqual(result['{} - {}'.format(today,today)], 1)
        self.assertNotEqual(result['{} - {}'.format(today,today)], 3)

    def test_get_month_fan_success(self):
        result = get_month_fan(self.user1)
        today = date.today().strftime('%Y-%m-%d')
        self.assertEqual(result['{} - {}'.format(today,today)], 2)

    def test_get_month_fan_fail(self):
        result = get_month_fan(self.user1)
        today = date.today().strftime('%Y-%m-%d')
        self.assertNotEqual(result['{} - {}'.format(today,today)],  1)
        self.assertNotEqual(result['{} - {}'.format(today,today)], 3)

    def test_get_month_comment_success(self):
        result = get_month_comment(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertEqual(result['{} - {}'.format(today,today)], 2)

    def test_get_month_comment_fail(self):
        result = get_month_comment(Videos.objects.filter(mup_id=1))
        today = date.today().strftime('%Y-%m-%d')
        self.assertNotEqual(result['{} - {}'.format(today,today)], 1)
        self.assertNotEqual(result['{} - {}'.format(today,today)], 3)

    def test_get_year_play_success(self):
        result = get_year_play(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertEqual(result[this_month], 10)

    def test_get_year_play_fail(self):
        result = get_year_play(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertNotEqual(result[this_month], 11)
        self.assertNotEqual(result[this_month], 9)

    def test_get_year_like_success(self):
        result = get_year_like(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertEqual(result[this_month], 2)

    def test_get_year_like_fail(self):
        result = get_year_like(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertNotEqual(result[this_month], 1)
        self.assertNotEqual(result[this_month], 3)

    def test_get_year_favor_success(self):
        result = get_year_favor(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertEqual(result[this_month], 2)

    def test_get_year_favor_fail(self):
        result = get_year_favor(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertNotEqual(result[this_month], 1)
        self.assertNotEqual(result[this_month], 3)

    def test_get_year_fan_success(self):
        result = get_year_fan(self.user1)
        this_month = date.today().strftime('%Y-%m')
        self.assertEqual(result[this_month], 2)

    def test_get_year_fan_fail(self):
        result = get_year_fan(self.user1)
        this_month = date.today().strftime('%Y-%m')
        self.assertNotEqual(result[this_month], 1)
        self.assertNotEqual(result[this_month], 3)

    def test_get_year_comment_success(self):
        result = get_year_comment(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertEqual(result[this_month], 2)

    def test_get_year_comment_fail(self):
        result = get_year_comment(Videos.objects.filter(mup_id=1))
        this_month = date.today().strftime('%Y-%m')
        self.assertNotEqual(result[this_month], 1)
        self.assertNotEqual(result[this_month], 3)

    def test_time_format_success(self):
        time = 1238952
        result = time_format(time)
        self.assertEqual(result, '344:09:12')

    def test_time_format_fail(self):
        time = 1238952
        result = time_format(time)
        self.assertNotEqual(result, '344:05:52')

    def test_serialize_data_success(self):
        result = serialize_data(self.video1, Follow.objects.filter(mup=self.user1).filter(is_active=True).values('fan'))
        self.assertNotEqual(result,{})

    def test_serialize_data_empty(self):
        result = serialize_data(self.video2, Follow.objects.filter(mup=self.user1).filter(is_active=True).values('fan'))
        self.assertEqual(result['play_count'],0)